package application;
	
import java.io.UnsupportedEncodingException;
import java.util.Base64;
import java.util.Base64.Decoder;

import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.stage.WindowEvent;
import util.DateUtil;
import util.Md5Util;
import util.PropertyUtil;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.image.Image;


public class Main extends Application {
	
	@Override
	public void start(Stage primaryStage) {
		try {
			
			Decoder decoder = Base64.getDecoder();
			String[] code = PropertyUtil.getKeyValue("code").replace("\\", "").split("=");
			String enddate = code[0]+'=';
			try {
				enddate = new String(decoder.decode(enddate), "UTF-8");
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			String token = code[1];
			String key = Md5Util.getMd5(enddate+Md5Util.getMd5("Aisino"));
			if(!key.equals(token) || Double.parseDouble(DateUtil.getNowStamp())>Double.parseDouble(DateUtil.getStamp(enddate,"yyyyMMdd"))+86400){
				RegisterForm.showAlertDialog();
				return;
			}else{
			Parent root = FXMLLoader.load(getClass().getClassLoader().getResource("view/FpMainView.fxml"));
			//Parent root = FXMLLoader.load(getClass().getClassLoader().getResource("view/CustomerView.fxml"));
			Scene scene = new Scene(root);
			primaryStage.setTitle("Moon开票助手正式版V3.9");
			//primaryStage.setResizable(false);
			primaryStage.getIcons().add(new Image("/image/change.png"));
			scene.getStylesheets().add(getClass().getClassLoader().getResource("css/application.css").toExternalForm());
			primaryStage.setScene(scene);
			primaryStage.show();
			}
		} catch(Exception e) {
			e.printStackTrace();
		}
		
		primaryStage.setOnCloseRequest(new EventHandler<WindowEvent>() {
			@Override
			public void handle(WindowEvent event) {
				if(AlertDialog.confirmDialog("退出", "确定退出程序？")){
					System.exit(0);
				}else{
					return;
				}
			}
		});
	}
	
	
	
	public static void main(String[] args) {
		launch(args);
	}

}
